import { defineStore } from "pinia";
import { AuthState } from "../interface";
import piniaPersistConfig from "@/stores/helper/persist";

// 授权
export const useAuthStore = defineStore({
  id: "AuthState",
  state: (): AuthState => ({
    // 用户按钮权限列表
    authButtons: {},
    // 路由权限列表
    authRouter: [],
    authRouterCodes: [],
    authMRACodes: []
  }),
  getters: {
    // 处理权限按钮数据，用于方便控制按钮
    authButtonsObj: state => {
      return state.authButtons;
    },
    // 后台返回的菜单数据，用于方便控制路由跳转时权限（这里已经处理成一维数组了）
    dynamicRouter: state => {
      return state.authRouter;
    },
    getAuthRouterCodes: state => {
      return state.authRouterCodes;
    },
    getAuthMRAMenu: state => {
      return state.authMRACodes;
    }
  },
  actions: {
    // setAuthButtons
    async setAuthButtons(authButtonList: { [key: string]: any }) {
      this.authButtons = authButtonList;
    },
    // setAuthRouter
    async setAuthRouter(dynamicRouter: string[]) {
      this.authRouter = dynamicRouter;
    },
    setAuthRouterCodes(dynamicRouterCodes: string[]) {
      this.authRouterCodes = dynamicRouterCodes;
    },
    setAuthMRACodes(MRACodes: Array<{ fullpath: string; code: string }>) {
      this.authMRACodes = MRACodes;
    }
  },
  persist: piniaPersistConfig("AuthState")
});
